package com.edu.web.sevlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.edu.entity.User;
import com.edu.service.UserService;
import com.edu.service.impl.UserServiceimpl;


@WebServlet("/UserServlet")
public class UserServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
   
    public UserServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("UTF-8");
		int pageSize=5;
		UserService userservice=new UserServiceimpl();
		int count=userservice.count();
		int pageNum=count%pageSize==0?count/pageSize:count/pageSize+1;
		request.setAttribute("pageNum", pageNum);
		String result=request.getParameter("currentPageNum");
		int currentPageNum=Integer.parseInt(result==null?"1":result);
		currentPageNum=currentPageNum>pageNum?1:currentPageNum;
		currentPageNum=currentPageNum<1?1:currentPageNum;
		request.setAttribute("currentPageNum", currentPageNum);
		currentPageNum=(currentPageNum-1)*pageSize;
		List<User> users=userservice.queryUser(currentPageNum,pageSize);
		request.setAttribute("userList", users);
		request.getRequestDispatcher("index.jsp").forward(request, response);
	}

	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		
	}

}
